import { Injectable } from "@angular/core";
import { HttpClient, HttpParams } from "@angular/common/http";
import { Observable } from "rxjs";
@Injectable({
  providedIn: 'root'
})
export class SellPromotionManageService {

  serviceUrls = {
    brandListUrl: "/coupon/goodsTypeAndBrand", // 查询指定品牌
    classificationListUrl: '/goodstypes/list', // 查询指定分类
    selectGoodsURL: "/goods/getGoodsByIdList", // 已选商品详情
    queryGoodsListUrl: '/goods/list/page', // 查询商品列表

    promotionListUrl: '/promotionInfo/list', // 直降促销管理
    promotionDetailsUrl: '/promotionInfo/details', // 直降促销详情
    promotionRecordUrl: '/promotionInfoLog/list', // 直降促销记录
    exportPromotionMainUrl: '/order/getActivityOrderExcel', // 直降记录--导出主信息
    exportPromotionDetailedUrl: '/order/getActivityOrderDetailsExcel', // 直降记录--导出明细信息

    cancelOrderUrl: '/order/cancelOrder', // 取消订单
    followerListUrl: "/follower/list", // 跟进订单查询
    followerAddUrl: '/follower/save', // 跟进新增

    addSalesPromotionUrl: '/promotionInfo/save', // 新增---直销促销活动
    updateSalesPromotionUrl: '/promotionInfo/update', // 修改---直销促销活动

    queryIdsGoodsUrl: '/goods/getGoodsByIdList', // 根据id查询商品列表
    uploadFileUrl: '/common/upload/file', // 上传
  }

  constructor(public httpClient: HttpClient) { }

  // 查询指定分类
  queryClassificationListData() {
    return this.httpClient.get(this.serviceUrls.classificationListUrl).toPromise();
  }

  // 查询指定品牌
  queryBrandListData() {
    return this.httpClient.get(this.serviceUrls.brandListUrl).toPromise();
  }

  // 查询商品list
  queryGoodsListData(params: any) {
    return this.httpClient.get(this.serviceUrls.queryGoodsListUrl, { params: params }).toPromise();
  }

  // 查询直降促销管理
  queryPromotionListData(paramas: any) {
    return this.httpClient.post(this.serviceUrls.promotionListUrl, paramas).toPromise();
  }

  // 查询直降促销详情
  queryPromotionDetailsData(params: any) {
    return this.httpClient.post(this.serviceUrls.promotionDetailsUrl, params).toPromise();
  }

  // 查询直降促销记录
  queryPromotionRecordData(params: any) {
    return this.httpClient.post(this.serviceUrls.promotionRecordUrl, params).toPromise();
  }

  // 导出直降记录主信息
  exportPromotionMainData(params: any) {
    return this.httpClient.get(this.serviceUrls.exportPromotionMainUrl, { params, responseType: 'blob' }).toPromise();
  }

  // 导出直降记录明细信息
  exportPromotionDetailedData(params: any) {
    return this.httpClient.get(this.serviceUrls.exportPromotionDetailedUrl, { params, responseType: 'blob' }).toPromise();
  }

  // 取消订单
  cancelOrderData(params: any) {
    return this.httpClient.post(this.serviceUrls.cancelOrderUrl, params).toPromise();
  }

  // 跟进订单查询
  queryFollowerListData(params: any) {
    return this.httpClient.post(this.serviceUrls.followerListUrl, params).toPromise();
  }

  // 跟进---新增
  addFollowerData(params: any) {
    return this.httpClient.post(this.serviceUrls.followerAddUrl, params).toPromise();
  }

  // 促销活动***新增
  addSalesPromotionData(params: any) {
    return this.httpClient.post(this.serviceUrls.addSalesPromotionUrl, params).toPromise();
  }
  // 促销活动***修改
  updateSalesPromotionData(params: any) {
    return this.httpClient.post(this.serviceUrls.updateSalesPromotionUrl, params).toPromise();
  }

  // 根据ids查询商品列表
  queryIdsGoodsListData(params: any) {
    return this.httpClient.post(this.serviceUrls.queryIdsGoodsUrl, params).toPromise();
  }

  // children数组合并
  conversionArrs(arrs: any) {
    let tempArr: any = [];
    arrs.map((ele: any) => {
      tempArr = tempArr.concat(ele.children);
    });
    return tempArr;
  }

  /**
   * 文件上传
   * @param fileToUpload 
   * @returns 
   */
  uploadFileData(fileToUpload: File, fileName?: any) {
    const formData: FormData = new FormData();
    formData.append('file', fileToUpload);
    formData.append('name', "order");
    if (fileName) {
      formData.append('fileName', fileName);
    }
    
    return this.httpClient.post(this.serviceUrls.uploadFileUrl, formData).toPromise();
  }
}
